package cn.iocoder.yudao.module.common.controller.admin.likes;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.common.controller.admin.likes.vo.*;
import cn.iocoder.yudao.module.common.dal.dataobject.likes.LikesDO;
import cn.iocoder.yudao.module.common.service.likes.LikesService;

@Tag(name = "管理后台 - 点赞")
@RestController
@RequestMapping("/common/likes")
@Validated
public class LikesController {

    @Resource
    private LikesService likesService;

    @PostMapping("/create")
    @Operation(summary = "创建点赞")
    public CommonResult<Long> createLikes(@Valid @RequestBody LikesSaveReqVO createReqVO) {
        return success(likesService.createLikes(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新点赞")
    public CommonResult<Boolean> updateLikes(@Valid @RequestBody LikesSaveReqVO updateReqVO) {
        likesService.updateLikes(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除点赞")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteLikes(@RequestParam("id") Long id) {
        likesService.deleteLikes(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得点赞")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<LikesRespVO> getLikes(@RequestParam("id") Long id) {
        LikesDO likes = likesService.getLikes(id);
        return success(BeanUtils.toBean(likes, LikesRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得点赞分页")
    public CommonResult<PageResult<LikesRespVO>> getLikesPage(@Valid LikesPageReqVO pageReqVO) {
        PageResult<LikesDO> pageResult = likesService.getLikesPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, LikesRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出点赞 Excel")
    @OperateLog(type = EXPORT)
    public void exportLikesExcel(@Valid LikesPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<LikesDO> list = likesService.getLikesPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "点赞.xls", "数据", LikesRespVO.class,
                        BeanUtils.toBean(list, LikesRespVO.class));
    }

}